﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Tanks.Contracts;
using Tanks;

namespace TanksGame
{
    class Tank : AnimatedSprite
    {
        public Directions Direction= Directions.Up;
        public int TypeOfTank { get; private set; }
        private SpriteEffects _spriteEffects;

        public Tank(MapItemType mapItemType, 
            Texture2D texture, int x, int y, int size, int numberOfFrames,int timeForFrame, int typeOfTank)
            : base(mapItemType, texture, x, y, size, numberOfFrames, timeForFrame)
        {
            TypeOfTank = typeOfTank;
        }

        public void Fire() 
        {

        }

        public void Move()
        {

        }

        public const int TankSizeInPx = 234;
        public override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(Texture, Rectangle, new Rectangle(TankSizeInPx * _currentFrame, TankSizeInPx * TypeOfTank, TankSizeInPx, TankSizeInPx), Color.Red,
                             GameScreen.DirectionToRadians[Direction], new Vector2(TankSizeInPx / 2, TankSizeInPx/2), _spriteEffects, 0);

        }

        public override void Update(Microsoft.Xna.Framework.GameTime gameTime, KeyboardState keyboardState)
        {
            int xDif = 0;
            int yDif = 0;
            _currentFrame = CalcCurrentFrame(gameTime);
            if (keyboardState.IsKeyDown(Keys.Up))
            {
                Direction = Directions.Up;
                xDif = 0;
                yDif = -1;
                _spriteEffects=SpriteEffects.None;
            }
            else if (keyboardState.IsKeyDown(Keys.Down))
            {
                Direction = Directions.Down;
                xDif = 0;
                yDif = 1;
                _spriteEffects=SpriteEffects.FlipHorizontally;
          
            }
            else if (keyboardState.IsKeyDown(Keys.Left))
            {
                Direction = Directions.Left;
                xDif = -1;
                yDif = 0;
                _spriteEffects=SpriteEffects.FlipHorizontally;
            }
            else if (keyboardState.IsKeyDown(Keys.Right))
            {
                Direction = Directions.Right;
                yDif = 0;
                xDif = 1;
                _spriteEffects=SpriteEffects.None;
            }

            base.Rectangle.X += 2*xDif;
            base.Rectangle.Y += 2*yDif;
        }
    }

}
